﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;
using System.Text.RegularExpressions;

namespace BookStore.Solution.Presentation
{
    public partial class FrmPublisher : DevComponents.DotNetBar.OfficeForm
    {
        private bool addNew = true;
        public bool viewDetail = false;
        Publisher publisher = null;
        PublisherManager publisherManager;

        public FrmPublisher()
        {
            InitializeComponent();
        }
        public FrmPublisher(string pubID, bool detail)
        {
            InitializeComponent();
            if (detail)
            {
                viewDetail = true;
                //btnSubmit.Visible = false;
                btnReset.Visible = false;
                btnSubmit.Text = "Đóng cửa sổ";
                btnSubmit.Width = 90;
                btnSubmit.Location = new Point(((int)this.Width/2)-45,btnSubmit.Location.Y);
                this.Text = "Xem thông tin nhà xuất bản: " + pubID;

            }
            else
            {
                this.Text = "Cập nhật thông tin nhà xuất bản: " + pubID;
                txtPublisherID.Enabled = false;
            }
            addNew = false;
            
            //Lấy thông tin nhà xuất bản
            publisherManager = new PublisherManager();
            publisher = publisherManager.GetDetail(pubID);
            //Hiển thị lên các textbox
            txtPublisherID.Text = publisher.PublisherID;
            txtPublisherName.Text = publisher.PublisherName;
            txtAddress.Text = publisher.Address;
            txtEmail.Text = publisher.Email;
            txtPhoneNumber.Text = publisher.PhoneNumber;
            txtWebsite.Text = publisher.Website;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (viewDetail)
            {
                this.Close();
            }
            else
            {
                string result = "", web = txtWebsite.Text;
                if (checkValidation())
                {
                    if (web == "http://")
                        web = "";
                    publisher = new Publisher();
                    publisher.PublisherID = txtPublisherID.Text;
                    publisher.PublisherName = txtPublisherName.Text;
                    publisher.Address = txtAddress.Text;
                    publisher.Email = txtEmail.Text;
                    publisher.PhoneNumber = txtPhoneNumber.Text;
                    publisher.Website = web;
                    publisher.Status = "Khả dụng";
                    publisherManager = new PublisherManager();
                    if (addNew)
                    {
                        result = publisherManager.Insert(publisher);
                    }
                    else
                    {
                        result = publisherManager.Update(publisher);
                    }
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Yêu cầu nhập đầy đủ thông tin", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool checkValidation()
        {
            if (txtPublisherID.Text == "")
            {
                lblPublisherID.ForeColor = Color.Red;
                return false;
            }
            if (txtPublisherName.Text == "")
            {
                lblPublisherName.ForeColor = Color.Red;
                return false;
            }
            if (txtPhoneNumber.Text != "")
            {
                string pattern = @"^-*[0-9,\.?\-?\(?\)?\ ]+$";
                if (!Regex.IsMatch(txtPhoneNumber.Text, pattern))
                {
                    lblPhoneNumber.ForeColor = Color.Red;
                    return false;
                }
            }
            return true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (addNew)
                txtPublisherID.ResetText();//Nếu là thêm mới thì cho reset
            txtPublisherName.ResetText();
            txtAddress.ResetText();
            txtPhoneNumber.ResetText();
            txtEmail.ResetText();
            txtWebsite.Text = "http://";
        }

        
    }
}